<?php

/**
 * Class RoleLogic
 */
class RoleLogic extends Model
{
    private $_modelRole = null;
    private $_customerId = null;

    public function __construct()
    {
        $this->_customerId = Session::$user['customerId'];
        Session::switchCustomer(0);

        $this->_modelRole = D('Role');

        Session::switchCustomer($this->_customerId);
    }


    /** --------------------------------------  增  -------------------------------------- */

    /**
     * 新建一个角色
     *
     * @param $data
     * @return mixed
     */
    public function addRole($data)
    {
        $result = $this->_modelRole->add($data);
        $sql = $this->_modelRole->getLastSql();
        R('Log/add',array('permission','','权限角色',op_add,$sql,'添加角色' . $data['chineseRole']));
        return $result;
    }

    /**
     * 批量添加
     *
     * @return bool|string
     */
    public function addAll($data)
    {
        return $this->_modelRole->addAll($data);
    }

    /** --------------------------------------  删  -------------------------------------- */

    /**
     * 根据id硬删除一个角色
     *
     * @param $roleId
     * @return mixed
     */
    public function deleteById($roleId)
    {
        $roleName = $this->_modelRole->where(['cid' => $this->_customerId, 'id' => $roleId])->getField('chineseRole');
        $result = $this->_modelRole->where(['cid' => $this->_customerId, 'id' => $roleId])->delete();
        $sql = $this->_modelRole->getLastSql();
        R('Log/add',array('permission','','权限角色',op_add,$sql,'删除角色' . $roleName));
        return $result;
    }

    /**
     * 根据where条件进行一些泛泛的删除操作
     *
     * @param $where
     * @return mixed
     */
    public function deleteWithWhere($where)
    {
        return $this->_modelRole->where($where)->delete();
    }

    /** --------------------------------------  改  -------------------------------------- */

    /** --------------------------------------  查  -------------------------------------- */

    /**
     * 根据id查询指定列内容
     *
     * @param        $roleId
     * @param string $field     指定列
     * @return mixed
     */
    public function getFieldById($roleId, $field)
    {
        return $this->_modelRole->where(['cid' => $this->_customerId, 'id' => $roleId])->field($field)->find();
    }

    /**
     * 查询单个列
     *
     * @param array  $where
     * @param string $field
     * @param bool   $list
     * @return mixed
     */
    public function getField($where = [], $field, $list = FALSE)
    {
        if (!isset($where['cid'])) $where['cid'] = $this->_customerId;
        return $this->_modelRole->where($where)->getField($field, $list);
    }

    /**
     * 查询角色列表, 需指定查询字段和查询条件
     *
     * @param string $fields
     * @param array  $where
     * @param string $order
     * @param string $orderBy
     * @param int    $start
     * @param int    $limit
     * @return mixed
     */
    public function selectPage($fields = '', $where = [], $order = 'id', $orderBy = 'desc', $start = 0, $limit = 10)
    {
        return $this->_modelRole
            ->where($where)
            ->field($fields)
            ->order("$order $orderBy")
            ->limit($start, $limit)
            ->select();
    }

    /**
     * 查询角色列表总个数, 需指定查询条件
     *
     * @param array  $where
     * @return mixed
     */
    public function selectPageCount($where = [])
    {
        return $this->_modelRole
            ->where($where)
            ->field('id')
            ->count();
    }

    /**
     * 指定条件查询指定列数据
     *
     * @param       $fields
     * @param array $where
     * @return mixed
     */
    public function selectFieldsWithWhere($fields, array $where)
    {
        return $this->_modelRole->where($where)->field($fields)->select();
    }
}